﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace Testcraft
{
    public class BlockBuffer : IDisposable
    {
        public struct VertexIndex
        {
            public int index;
            public int refCount;
        }

        public int id;
        public GraphicsDevice device;
        public DynamicVertexBuffer vbuffer;
        public IndexBuffer ibuffer;
        public int ibufferSize;
        public int vbufferSize;
        public short[, , ,] indices;

        List<PackedVertexPositionNormalTexture> currentVertices;
        List<Int16> currentIndices;

        public BlockBuffer(int id, GraphicsDevice device, int chunkPartWidth, int chunkPartHeight)
        {
            if (device.IsDisposed)
                return;
            this.id = id;
            this.device = device;
            //this.vbufferContents = new List<PackedVertexPositionNormalTexture>();
            this.vbuffer = null;
            this.ibuffer = null;

            this.vbufferSize = 256;
            this.ibufferSize = 256;

            this.indices = new short[chunkPartWidth + 1, chunkPartHeight + 1, chunkPartWidth + 1, 6];

            this.InitializeIBuffer();
            this.InitializeVBuffer();

            this.device.DeviceReset += new EventHandler<EventArgs>(device_DeviceReset);
        }

        void device_DeviceReset(object sender, EventArgs e)
        {
            this.Rebuild(this.currentVertices, this.currentIndices);
        }

        private void InitializeIBuffer()
        {
            if (this.ibuffer != null)
                this.ibuffer.Dispose();

            this.ibuffer = new DynamicIndexBuffer(this.device, IndexElementSize.SixteenBits, this.ibufferSize, BufferUsage.WriteOnly);
        }

        private void InitializeVBuffer()
        {
            if (this.vbuffer != null)
                this.vbuffer.Dispose();

            this.vbuffer = new DynamicVertexBuffer(this.device, PackedVertexPositionNormalTexture.VertexDeclaration, this.vbufferSize, BufferUsage.WriteOnly);
        }

        public void RebuildVBuffer(List<PackedVertexPositionNormalTexture> vertices)
        {
            if (this.vbufferSize != vertices.Count)
            {
                this.vbufferSize = vertices.Count;

                this.InitializeVBuffer();
            }

            this.vbuffer.SetData<PackedVertexPositionNormalTexture>(vertices.ToArray());
        }

        public void Rebuild(List<PackedVertexPositionNormalTexture> vertices, List<Int16> indices)
        {
            this.currentVertices = vertices;
            this.currentIndices = indices;
            if (this.vbufferSize != vertices.Count)
            {
                this.vbufferSize = vertices.Count;

                this.InitializeVBuffer();
            }

            if (this.ibufferSize != indices.Count)
            {
                this.ibufferSize = indices.Count;

                this.InitializeIBuffer();
            }

            this.vbuffer.SetData<PackedVertexPositionNormalTexture>(vertices.ToArray());
            this.ibuffer.SetData<Int16>(indices.ToArray());
        }

        //public void RebuildVBuffer()
        //{
        //    if (this.vbufferSize != this.vbufferContents.Count)
        //    {
        //        this.vbufferSize = this.vbufferContents.Count;

        //        this.InitializeVBuffer();
        //    }

        //    this.vbuffer.SetData<PackedVertexPositionNormalTexture>(this.vbufferContents.ToArray());
        //    this.flags &= ~BufferFlags.VBufferDirty;
        //}

        public void Dispose()
        {
            this.device.DeviceReset -= this.device_DeviceReset;
            if (this.vbuffer != null)
                this.vbuffer.Dispose();

            if (this.ibuffer != null)
                this.ibuffer.Dispose();
        }
    }
}
